/*
 * Copyright (C) Francisco Morero Peyrona. All rights reserved.
 *
 * This software is published under the terms of Open Source
 * License version 1.1, a copy of which has been included with this
 * distribution in the License.txt file.
 */

package jdbcmanager.dbtools.agentwise;

import java.io.*;
import java.net.*;
import java.sql.*;

import jdbcmanager.app.*;

/**
 * Load a JDBC driver at run-time.
 * 
 * @author f.morero
 */
final class DriverLoader
{   
   static Driver load( String sClass, String[] asJAR ) 
      throws ClassNotFoundException, InstantiationException, IllegalAccessException
   {
      Driver driver = null;
      
      URL[] aURL = new URL[ asJAR.length ];
      
      for( int n = 0; n < aURL.length; n++ )
      {
         File f = new File( asJAR[n] );
         
         try
         {
            aURL[n] = f.toURL();
         }
         catch( MalformedURLException exc )
         {
            App.showDebugException( exc );
         }
      }
      
      URLClassLoader ucl = new URLClassLoader( aURL );
      
      Class clazz = ucl.loadClass( sClass );
      
      if( clazz != null )
         driver = (Driver) clazz.newInstance();
      
      return driver;
   }
}